package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class FoodListActivity extends AppCompatActivity implements View.OnClickListener {

    ListView Lv_Food;
    Button Btn_Back;
    private Intent uid;
    private static final String TAG = "FoodListActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_list);

        Lv_Food = findViewById(R.id.Lv_Food);

        Btn_Back = findViewById(R.id.Btn_Back);

        Btn_Back.setOnClickListener(this);

        show();
        Lv_Food.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                TextView tv = view.findViewById(R.id.Lv_Food);
//                int sid = Integer.parseInt(String.valueOf(tv.getText()));
                Log.e(TAG, "onItemClick: "+position );
                Intent intent = new Intent(FoodListActivity.this,DetailsActivity.class);
                intent.putExtra("id", (position+1));
                intent.putExtra("uid",uid);
                startActivity(intent);
            }
        });
    }

    private void show() {
        new Thread(new Runnable() {
            private static final String TAG = "MainActivity";

            @Override
            public void run() {
                //1 client对象
                OkHttpClient client = new OkHttpClient();
                //2 Request对象
                FormBody body = new FormBody.Builder()
//                        .add("telphone",Ed_phone_eT.getText().toString())
                        .build();

                Request request = new Request.Builder()
                        .url("http://10.2.64.68:8080/juyuan/foods/list")
                        .post(body)
                        .build();

                //3 call对象
                Call call = client.newCall(request);
                //4 执行call对象得到数据
                try {
                    Response response = call.execute();

                    String data = response.body().string();

                    Gson gson = new Gson();
                    FoodGson gf = gson.fromJson(data, FoodGson.class);
                    final List<String> names = new ArrayList<>();
                    List<FoodGson.ListBean> listBeans = gf.getList();

                    for (int i = 0; i < listBeans.size(); i++) {
                        names.add(listBeans.get(i).getName());
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(FoodListActivity.this, android.R.layout.simple_list_item_1, names);
                            Lv_Food.setAdapter(arrayAdapter);
                        }
                    });


                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }).start();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.Btn_Back:
                Intent intent = new Intent(FoodListActivity.this, LoginActivity.class);
                startActivity(intent);
                break;
        }
    }
}